package cn.interestingshop.dao.impl;

import cn.interestingshop.dao.BaseDao;
import cn.interestingshop.dao.ClassifyDao;
import cn.interestingshop.entity.Classify;
import cn.interestingshop.vo.OrderInfoVo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ClassifyDaoImpl extends BaseDao implements ClassifyDao {
    @Override
    public List<Classify> selectAll(int pageNo, int pageSize) {
        List<Classify> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //1. 加载驱动
            //2. 获取连接对象
            conn = getConnection();
            //3. 定义sql语句
            String sql = "SELECT a.*,b.classify_name as parent_name FROM `t_classify` a left join t_classify b " +
                    "on a.parent_id=b.id LIMIT ?, ?";
            //4. 创建执行对象
            ps = conn.prepareStatement(sql);
            //5. 填充参数
            ps.setInt(1, (pageNo-1)*pageSize);
            ps.setInt(2,pageSize);
            //6. 执行sql语句
            rs = ps.executeQuery();
            //7. 分析返回结果
            while (rs.next()) {
                Classify classify=new Classify();
                classify.setId(rs.getInt("id"));
                classify.setClassifyName(rs.getString("classify_name"));
                classify.setParentId(rs.getInt("parent_id"));
                classify.setType(rs.getInt("type"));
                classify.setIcon(rs.getString("icon"));
                classify.setParentName(rs.getString("parent_name"));

                list.add(classify);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //8.释放资源关闭连接
            closeAll(rs, ps, conn);
        }
        return list;
    }

    @Override
    public int selectCount() {
        int count=0;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //1. 加载驱动
            //2. 获取连接对象
            conn = getConnection();
            //3. 定义sql语句
            String sql = "SELECT count(1) FROM `t_classify`";
            //4. 创建执行对象
            ps = conn.prepareStatement(sql);
            //5. 填充参数
            //6. 执行sql语句
            rs = ps.executeQuery();
            //7. 分析返回结果
            while (rs.next()) {
                count=rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //8.释放资源关闭连接
            closeAll(rs, ps, conn);
        }
        return count;
    }

    @Override
    public List<Classify> selectAll(int parentId) {
        List<Classify> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //1. 加载驱动
            //2. 获取连接对象
            conn = getConnection();
            //3. 定义sql语句
            String sql = "SELECT * FROM `interesting_shop_db`.`t_classify` WHERE `parent_id` = ? ";
            //4. 创建执行对象
            ps = conn.prepareStatement(sql);
            //5. 填充参数
            ps.setInt(1, parentId);
            //6. 执行sql语句
            rs = ps.executeQuery();
            //7. 分析返回结果
            while (rs.next()) {
                Classify classify=new Classify();
                classify.setId(rs.getInt("id"));
                classify.setClassifyName(rs.getString("classify_name"));
                classify.setParentId(rs.getInt("parent_id"));
                classify.setType(rs.getInt("type"));
                classify.setIcon(rs.getString("icon"));

                list.add(classify);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //8.释放资源关闭连接
            closeAll(rs, ps, conn);
        }
        return list;
    }

    @Override
    public int insert(Classify classify) {
        String sql="INSERT INTO `interesting_shop_db`." +
                "`t_classify`(`classify_name`, `parent_id`, `type`, `icon`) VALUES (?, ?, ?, ?)";
        return executeUpdate(sql,new Object[]{
                classify.getClassifyName(),
                classify.getParentId(),
                classify.getType(),
                classify.getIcon()
        });
    }

    @Override
    public Classify selectOne(int id) {
        Classify classify = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //1. 加载驱动
            //2. 获取连接对象
            conn = getConnection();
            //3. 定义sql语句
            String sql = "SELECT * FROM `interesting_shop_db`.`t_classify` WHERE `id` = ? ";
            //4. 创建执行对象
            ps = conn.prepareStatement(sql);
            //5. 填充参数
            ps.setInt(1, id);
            //6. 执行sql语句
            rs = ps.executeQuery();
            //7. 分析返回结果
            while (rs.next()) {
                classify=new Classify();
                classify.setId(rs.getInt("id"));
                classify.setClassifyName(rs.getString("classify_name"));
                classify.setParentId(rs.getInt("parent_id"));
                classify.setType(rs.getInt("type"));
                classify.setIcon(rs.getString("icon"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //8.释放资源关闭连接
            closeAll(rs, ps, conn);
        }
        return classify;
    }

    @Override
    public int update(Classify classify) {
        String sql="UPDATE `interesting_shop_db`.`t_classify` " +
                "SET `classify_name` = ?, `parent_id` = ?, `type` = ? " +
                "WHERE `id` = ?";
        return executeUpdate(sql,new Object[]{
                classify.getClassifyName(),
                classify.getParentId(),
                classify.getType(),
                classify.getId()
        });
    }
}
